<?php

/**
 * 网站配置模型
 * @copyright (c) 2013, Mr.Wang
 * 
 */
class ConfigModel extends CommonModel {

    public function get() {
        return $this->getField("varname,value");
    }

    public function config_cache() {
        $data = $this->getField("varname,value");
        foreach (C("TMPL_PARSE_STRING") as $key => $value) {
            $data['TPL_' . $key] = $value;
        }

        $define = "<?php \r\n";
        foreach ($data as $key => $value) {
            $key = 'CONFIG_' . strtoupper($key);
            $define.="define('{$key}', '{$value}');\r\n";
        }


        file_put_contents(RUNTIME_PATH . "define.php", $define);
        return $data;
    }

    /**
     * 后台有更新/编辑则删除缓存
     * @param type $data
     */
    public function _before_write($data) {
        parent::_before_write($data);
        F("Config", NULL);
    }

    //删除操作时删除缓存
    public function _after_delete($data, $options) {
        parent::_after_delete($data, $options);
        $this->config_cache();
    }

    //更新数据后更新缓存
    public function _after_update($data, $options) {
        parent::_after_update($data, $options);
        $this->config_cache();
    }

    //插入数据后更新缓存
    public function _after_insert($data, $options) {
        parent::_after_insert($data, $options);
        $this->config_cache();
    }

}

?>
